<template>
  <v-dialog :title="title" :close-on-click-modal="false" v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''" :disabled="method==='view'" label-width="140px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同名称" prop="contractName" :rules="[ {required: true, message:'合同名称不能为空', trigger:'blur'}]">
            <el-input v-model="inputForm.contractName" placeholder="请填写合同名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同编号" prop="contractNo" :rules="[{required: true, message:'合同编号不能为空', trigger:'blur'} ]">
            <el-input v-model="inputForm.contractNo" placeholder="请填写合同编号"></el-input>
          </el-form-item>
        </el-col>
    
          <el-col :span="12">
            <el-form-item label="类型" prop="downType"
                :rules="[{required: true, message:'类型不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.downType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('claim_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同类别" prop="contractCategory"
                :rules="[{required: true, message:'合同类别不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.contractCategory" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('contract_category')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同金额(元)" prop="contractAmount" :rules="[{required: true, message:'合同金额不能为空', trigger:'blur'},{message:'合同金额为数字',validator: validator.isNumber}
                 ]">
            <el-input v-model="inputForm.contractAmount" placeholder="请填写合同金额"></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="修正后合同金额(元)" prop="correctContractAmount"  :rules="[{validator: validator.isNumber}
                 ]">
            <el-input v-model="inputForm.correctContractAmount" placeholder="请填写合同金额"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="供应商" prop="supplier.id" :rules="[{required: true, message:'供应商不能为空', trigger:'blur'}
                 ]">
            <GridSelect title="选择供应商" labelName='companyName' labelValue='id' v-model="inputForm.supplier.id"  :columns="[
         
            {
              prop: 'companyName',
              label: '单位名称'
            }
            ]" :searchs="[
          
            {
              prop: 'companyName',
              label: '单位名称'
            }
            ]" :api="correspondentService">
            </GridSelect>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税率(%)" prop="taxRate" :rules="[{validator: validator.isNumber}
                 ]">
            <el-input v-model="inputForm.taxRate" placeholder="请填写税率"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税额" prop="taxAmount" :rules="[{validator: validator.isNumber}
                 ]">
            <el-input v-model="inputForm.taxAmount" placeholder="请填写税额"></el-input>
          </el-form-item>
        </el-col>
             <el-col :span="12">
          <el-form-item label="质保金" prop="qualityGuaranteeFund" :rules="[{validator: validator.isNumber}
                 ]">
            <el-input v-model="inputForm.qualityGuaranteeFund" placeholder="请填写质保金"></el-input>
          </el-form-item>
        </el-col>
     
        <el-col :span="12">
          <el-form-item label="合同签订时间" prop="contractSignTime" :rules="[
                 ]">
            <el-date-picker style="width: 100%;" v-model="inputForm.contractSignTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同生效时间" prop="contractBeginTime" :rules="[
                 ]">
            <el-date-picker style="width: 100%;" v-model="inputForm.contractBeginTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同结束时间" prop="contractEndTime" :rules="[
                 ]">
            <el-date-picker style="width: 100%;" v-model="inputForm.contractEndTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
      
  


      
        <el-col :span="24">
          <el-form-item label="附件" prop="attachment" :rules="[
                 ]">
            <file-upload v-model="inputForm.attachment" tip="单个文件不要超过200M"></file-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark" :rules="[
                 ]">
            <el-input type="textarea" v-model="inputForm.remark" placeholder="请填写备注" :rows="6"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false" icon="circle-close">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
      </span>
    </template>
  </v-dialog>
</template>

<script>
import GridSelect from '@/components/gridSelect'
import contractService from '@/api/contract/tPurchaseContractService'
import purchaseService from '@/api/purchase/purchaseService'
import projectMainService from '@/api/project/projectMainService'
import correspondentService from '@/api/correspondent/correspondentService'
export default {
  data() {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      purchaseService: purchaseService,
      projectMainService: projectMainService,
      correspondentService: correspondentService,
      inputForm: {
        id: '',
        purchase: {
          id: ''
        },
        project: {
          id: ''
        },
        contractNo: '',
        contractName: '',
        contractObject: {
          id: ''
        },
        predictContractId:'',
        supplier:{id:""},
        contractType: '',
        contractAmount: '',
        taxRate: '',
        taxAmount: '',
        contractSignTime: '',
        contractBeginTime: '',
        contractEndTime: '',
        attachment: '',
        remark: '',
        qualityGuaranteeFund: '',
        contractFeature:2,
           finalContractName:'',
      }
    }
  },
  components: {
    GridSelect
  },
   watch: {
    'inputForm.attachment': {
      handler(newVal) {
        if (newVal) {
          // 获取所有文件信息
          const files = newVal.split(',');
          const fileNames = [];
          const fileUrls = [];

          files.forEach(file => {
            if (file&&file.indexOf(',')) {
              const urlArr = file.split('&fileName=');
              if (urlArr.length >= 2) {
                const fileName = urlArr[urlArr.length - 1];
                const fileUrl = urlArr[0];
                fileNames.push(fileName);
                fileUrls.push(fileUrl);
              } else {
                return;
              }
            }
          });         
          if (fileNames.length) {
            this.inputForm. finalContractName = fileNames.join(',');
            // 保存纯文件地址（不带文件名）
            // this.inputForm.attachment = fileUrls.join(',');
          }
        }
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    init(method, id) {
      this.method = method

      if (method === 'add') {
        this.inputForm.id = '';
        this.inputForm.purchase.id = id.purchaseId;
        this.inputForm.project.id = id.projectId;
        this.inputForm.predictContractId= id.id;
        this.title = `新建合同管理`
      } else if (method === 'edit') {
         
        this.inputForm.id = id
        this.title = '修改合同管理'
      } else if (method === 'view') {
        this.inputForm.id = id
        this.title = '查看合同管理'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          contractService.queryById(this.inputForm.id).then((data) => {
            this.inputForm = {...this.inputForm, ...data}
            this.inputForm.supplier.tenantDTO={}
            this.loading = false
          })
        }
      })
    },
    // 表单提交
    doSubmit() {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          contractService.save(this.inputForm).then((data) => {
            this.visible = false
            this.$message.success(data)
            this.$emit('refreshDataList')
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

  
